package com.aiwiown.snackmq.console.web;

import com.aiwiown.snackmq.common.dto.console.ClusterOverviewDTO;
import com.aiwiown.snackmq.console.service.SnackMQAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 为前端 AJAX 请求提供 JSON 数据的 API 控制器。
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class DashboardApiController {

    @Autowired
    private SnackMQAdminService adminService;

    @GetMapping("/cluster-overview")
    public ResponseEntity<?> getClusterOverview() {
        try {
            ClusterOverviewDTO overview = adminService.getClusterOverview();
            return ResponseEntity.ok(overview);
        } catch (Exception e) {
            log.warn("Failed to fetch cluster overview for API: {}", e.getMessage());
            // 返回一个包含错误信息的对象，方便前端处理
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                                 .body("Failed to fetch cluster overview: " + e.getMessage());
        }
    }
}